package com.his.config;

import org.apache.shiro.authc.*;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.springframework.stereotype.Component;

@Component
public class JwtRealm extends AuthenticatingRealm {

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JwtToken;
    }

    /**
     * 认证
     *
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("我是jwt的realm认证");
        boolean verifyResult = JwtUtils.verify((String) token.getCredentials());
        if (!verifyResult) {
            throw new IncorrectCredentialsException();
        }
        return new SimpleAuthenticationInfo(
                token.getPrincipal(),
                token.getCredentials(), "JwtRealm");
    }

/**
 * 授权
 */
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        System.out.println("我是jwt的realm授权");
//
//        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//
//        Subject subject = SecurityUtils.getSubject();
//        User user = null;
//        try {
//            user = (User) subject.getPrincipal();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        String right = user.getRight();
//        String[] split = right.split(",");
//        for (String s : split) {
//            simpleAuthorizationInfo.addStringPermission(s);
//        }
//
//        return simpleAuthorizationInfo;
//    }


}
